package com.help10000.rms.ui.fragments.user

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.amir.common.beans.MsgListData
import com.amir.common.utils.LogUtils
import com.help10000.rms.R
import com.help10000.rms.mvp.presenter.MessageListPresenter
import com.help10000.rms.mvp.view.MessageDetailView
import com.help10000.rms.ui.adapter.MessageListAdapter
import com.help10000.rms.ui.base.BaseFragment
import com.scwang.smartrefresh.layout.header.ClassicsHeader
import kotlinx.android.synthetic.main.fragment_message.*


class MessageFragment : BaseFragment<MessageListPresenter, MessageDetailView>(), MessageDetailView {


    private var page = 1
    private var msgType = 1
    var mAdapter: MessageListAdapter? = null

    companion object {
        fun newInstance(msgType: Int): MessageFragment {
            val fragment = MessageFragment()
            fragment.msgType = msgType
            return fragment
        }
    }

    override fun getRefreshTag(): String {
        return super.getRefreshTag() + "_" + msgType;
    }

    override fun lazyLoad() {
        mPresenter.getMessageList(msgType, page)
    }

    override fun getlayoutId(): Int {
        return R.layout.fragment_message
    }

    override fun initView(rootView: View?) {
        initRefreshLayout(refresh)
        (refresh.refreshHeader as ClassicsHeader).setAccentColor(mContext.resources.getColor(R.color.white))
    }

    override fun onDataSuccess(data: MsgListData) {
        onRefreshFinish(true)
        onLoadMoreFinish(true)
        if (page == 1) {
            if (data.TotalQty == 0) {
                tv_empty.visibility = View.VISIBLE
                return
            }
            tv_empty.visibility = View.GONE
            mAdapter = MessageListAdapter(context, data.AppMessageList)
            rv_list.apply {
                itemAnimator = null
                layoutManager = LinearLayoutManager(context)
                adapter = mAdapter
            }
        } else {
            if (data.AppMessageList.isEmpty()) {
                onFinishNoMore()
                return
            }
            var oldCount = mAdapter?.itemCount
            mAdapter?.addData(data.AppMessageList)
            mAdapter?.notifyItemRangeChanged(oldCount!!, data.AppMessageList.size)
        }
    }


    override fun initData(savedInstanceState: Bundle?) {

    }

    override fun showLoading() {
        super.showLoading()
        LogUtils.E("MSG  LOADING" + msgType)
    }

    override fun loadMore() {
        page++
        mPresenter.getMessageList(msgType, page)
    }

    override fun downOnRefresh() {
        page = 1
        mPresenter.getMessageList(msgType, page)
    }

    override fun initListener() {

    }

    override fun createPresenter(): MessageListPresenter {
        return MessageListPresenter(this)
    }


}